package com.jeeplus.test.jlnu.student.service.impl;


import com.alibaba.druid.sql.visitor.functions.If;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.test.jlnu.student.domain.entity.ApprovalForm;
import com.jeeplus.test.jlnu.student.domain.entity.BkbsssStudentObtain;
import com.jeeplus.test.jlnu.student.domain.entity.StudentInfoApprove;
import com.jeeplus.test.jlnu.student.mapper.BkbsssStudentObtainMapper;
import com.jeeplus.test.jlnu.student.mapper.StudentInfoApproveMapper;
import com.jeeplus.test.jlnu.student.service.StudentInfoApproveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class StudentInfoApproveServiceImpl implements StudentInfoApproveService {

    @Autowired
    private StudentInfoApproveMapper studentInfoApproveMapper;
    @Autowired
    private BkbsssStudentObtainMapper studentObtainMapper;

    @Override
    public List<StudentInfoApprove> getOutside(String studentId) {
        return studentInfoApproveMapper.getOutside(studentId);
    }

    @Override
    public StudentInfoApprove getStudentInfo(String studentId, String outsideType,String flag) {
        return studentInfoApproveMapper.getStudentInfo(studentId,outsideType,flag);
    }

    @Override
    public int updateType(ApprovalForm approvalForm) {
        return studentInfoApproveMapper.updateType(approvalForm);
    }

    @Override
    public Page<StudentInfoApprove> getOutsidePage(StudentInfoApprove studentInfoApprove, Page<StudentInfoApprove> page) {

        // 查询分组后的数据
        List<StudentInfoApprove> list = studentInfoApproveMapper.getOutsidePage(studentInfoApprove, page);
        // 查询去重后的总数
        Long total = studentInfoApproveMapper.countDistinctStudentId(studentInfoApprove);
        // 手动构造分页结果
        // ✅ 正确构造 Page 对象
        Page<StudentInfoApprove> resultPage = new Page<>(page.getCurrent(), page.getSize(), total);
        resultPage.setRecords(list); // 手动设置数据列表
        return resultPage;
    }

    @Override
    public List<StudentInfoApprove> getByStatus(String studentId, int approvalStatus) {
        return studentInfoApproveMapper.getByStatus(studentId,approvalStatus);
    }

    @Override
    public int approve(ApprovalForm approvalForm) {
        String studentId = approvalForm.getStudentId();
        int i = studentInfoApproveMapper.batchUpdateByEntityType(approvalForm);
        // 修改就业信息状态
        if ("pass".equals(approvalForm.getType())) {
            LambdaUpdateWrapper<BkbsssStudentObtain> wrapper = Wrappers.<BkbsssStudentObtain>lambdaUpdate()
                    .eq(BkbsssStudentObtain::getStudentId, studentId);
            BkbsssStudentObtain bkbsssStudentObtain = new BkbsssStudentObtain();
            bkbsssStudentObtain.setApprovalStatus(1);
            studentObtainMapper.update(bkbsssStudentObtain,wrapper);
        }else {
            LambdaUpdateWrapper<BkbsssStudentObtain> wrapper = Wrappers.<BkbsssStudentObtain>lambdaUpdate()
                    .eq(BkbsssStudentObtain::getStudentId, studentId);
            BkbsssStudentObtain bkbsssStudentObtain = new BkbsssStudentObtain();
            bkbsssStudentObtain.setApprovalStatus(2);
            bkbsssStudentObtain.setRejectReason(null);
            studentObtainMapper.update(bkbsssStudentObtain,wrapper);
        }
        return i;
    }
}
